﻿using System;
using System.Globalization;
using BGShell.Drawing;
using BGShell.Components;

namespace BGShell.PowerShell.DataTips
{
    internal sealed class EnumFieldDataTip : PSObjectRawDataTip
    {
        private readonly String _format;

        public EnumFieldDataTip(Type enumType, String name, Object value)
            : base(name, value)
        {
            var flags = HasFlagsAttribute(enumType);
            
            if (flags)
            {
                Value = ConvertToUnderlyingUnsignedType(value);
            }
            else
            {
                Value = ConvertToUnderlyingType(value);
            }

            _format = GetFormatString(value, flags);
            Icon = TypeModelIcon.EnumValue;
        }

        public override string ValueAsString
        {
            get
            {
                var fmt = (Value as IFormattable);

                if (fmt != null)
                {
                    string prefix = string.Empty;

                    if (_format != null)
                    {
                        prefix = "0x";
                    }

                    return prefix + fmt.ToString(_format, CultureInfo.CurrentCulture);
                }

                return base.ValueAsString;
            }
        }

        private static string GetFormatString(object obj, bool flags)
        {
            if ((obj != null) && (flags))
            {
                switch (Convert.GetTypeCode(obj))
                {
                    case TypeCode.SByte:
                    case TypeCode.Byte:
                        return "x2";

                    case TypeCode.Int16:
                    case TypeCode.UInt16:
                        return "x4";

                    case TypeCode.Int32:
                    case TypeCode.UInt32:
                        return "x8";

                    case TypeCode.Int64:
                    case TypeCode.UInt64:
                        return "x16";
                }
            }

            return null;
        }

        private static object ConvertToUnderlyingType(object value)
        {
            if (value != null)
            {
                switch (Convert.GetTypeCode(value))
                {
                    case TypeCode.SByte:
                        return Convert.ToSByte(value, CultureInfo.InvariantCulture);

                    case TypeCode.Int16:
                        return Convert.ToInt16(value, CultureInfo.InvariantCulture);

                    case TypeCode.Int32:
                        return Convert.ToInt32(value, CultureInfo.InvariantCulture);

                    case TypeCode.Int64:
                        return Convert.ToInt64(value, CultureInfo.InvariantCulture);

                    case TypeCode.Byte:
                        return Convert.ToByte(value, CultureInfo.InvariantCulture);

                    case TypeCode.UInt16:
                        return Convert.ToUInt16(value, CultureInfo.InvariantCulture);

                    case TypeCode.UInt32:
                        return Convert.ToUInt32(value, CultureInfo.InvariantCulture);

                    case TypeCode.UInt64:
                        return Convert.ToUInt64(value, CultureInfo.InvariantCulture);
                }
            }

            return value;
        }
        private static object ConvertToUnderlyingUnsignedType(object value)
        {
            if (value != null)
            {
                switch (Convert.GetTypeCode(value))
                {
                    case TypeCode.SByte:
                        return unchecked((Byte)(Convert.ToSByte(value, CultureInfo.InvariantCulture)));

                    case TypeCode.Int16:
                        return unchecked((UInt16)(Convert.ToInt16(value, CultureInfo.InvariantCulture)));

                    case TypeCode.Int32:
                        return unchecked((UInt32)(Convert.ToInt32(value, CultureInfo.InvariantCulture)));

                    case TypeCode.Int64:
                        return unchecked((UInt64)(Convert.ToInt64(value, CultureInfo.InvariantCulture)));

                    case TypeCode.Byte:
                        return Convert.ToByte(value, CultureInfo.InvariantCulture);

                    case TypeCode.UInt16:
                        return Convert.ToUInt16(value, CultureInfo.InvariantCulture);
                    
                    case TypeCode.UInt32:
                        return Convert.ToUInt32(value, CultureInfo.InvariantCulture);

                    case TypeCode.UInt64:
                        return Convert.ToUInt64(value, CultureInfo.InvariantCulture);
                }
            }

            return value;
        }

        private static bool HasFlagsAttribute(Type t)
        {
            var attrs = t.GetCustomAttributes(typeof(FlagsAttribute), false);

            return (attrs != null) && (attrs.Length > 0);
        }
    }
}
